package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.SetMeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;
    private final DishMapper dishMapper;
    private final SetMealMapper setMealMapper;

    public CategoryServiceImpl(@Autowired CategoryMapper categoryMapper, @Autowired DishMapper dishMapper, @Autowired SetMealMapper setMealMapper) {
        this.categoryMapper = categoryMapper;
        this.dishMapper = dishMapper;
        this.setMealMapper = setMealMapper;
    }

    //分页查询分类
    @Override
    public PageResult queryPageOnConditional(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.queryPageOnConditional(categoryPageQueryDTO.getName(), categoryPageQueryDTO.getType());
        return new PageResult(page.getTotal(), page.getResult());
    }

    //新增分类
    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                .type(categoryDTO.getType())
                .status(StatusConstant.DISABLE)
                .build();
        categoryMapper.insert(category);
    }

    //启用禁用分类
    @Override
    public void startOrStop(Integer status, Long id) {
        // 构建分类对象
        Category category = Category.builder()
                .id(id).status(status)
                .build();
        categoryMapper.update(category);
    }

    //修改分类
    @Override
    public void update(CategoryDTO categoryDTO) {
        // 构建分类对象
        Category category = Category.builder()
                .id(categoryDTO.getId())
                .sort(categoryDTO.getSort())
                .name(categoryDTO.getName())
                .type(categoryDTO.getType())
                .build();
        categoryMapper.update(category);
    }

    //根据id删除分类
    @Override
    public void deleteById(Long id) {
        //删除分类时，需要判断该分类是否关联了菜品或套餐，所以需要先查询分类关联了菜品或套餐，再进行删除
        int dishRows = dishMapper.selectByCategoryId(id);
        int setMealRows = setMealMapper.selectByCategoryId(id);
        if (dishRows > 0) {
            //分类下关联了菜品，不允许删除,抛出业务异常,给予提示
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        if (setMealRows > 0) {
            //分类下关联了套餐，不允许删除,抛出业务异常,给予提示
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //没有关联菜品或套餐，可以删除
        categoryMapper.deleteById(id);
    }

    //根据类型查询分类
    @Override
    public List<Category> getByType(Integer type) {
        return categoryMapper.selectByType(type);
    }
}
